<template>
<el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="30%">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-position="top">
        <el-form-item label="设备名称" prop="title">
            <el-input v-model="ruleForm.title" placeholder="请填写设备名称"></el-input>
        </el-form-item>

        <el-form-item label="UUID" prop="uuid">
            <el-input v-model="ruleForm.uuid" placeholder="请填写uuid"></el-input>
        </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
    </div>
</el-dialog>
</template>

<script>
import {
    updateApi
} from "@/api/task/device";

const ruleForm = {
    title: "",
    uuid: "",
};

export default {
    data() {
        return {
            dialog: {
                title: "",
                visible: false,
                type: "",
            },
            fastVal: "",
            ruleForm: Object.assign({}, ruleForm),
            rules: {
                title: [{
                        required: true,
                        message: "请输入设备名称",
                        trigger: "blur",
                    },
                    {
                        min: 2,
                        max: 30,
                        message: "长度在 2 到 30 个字符",
                        trigger: "blur",
                    },
                ],
                uuid: [{
                        required: true,
                        message: "请输入uuid",
                        trigger: "taggle",
                    },

                ],
            },
        };
    },
    methods: {
        showUpdateView(row) {
            let dialog = {
                title: "更新设备信息",
                visible: true,
                type: "create",
            };
            this.dialog = dialog;
            // 初始化表单
            this.ruleForm = {...row};
        },
        
        submit() {
            this.$refs["ruleForm"].validate((valid) => {
                updateApi(this.ruleForm)
                        .then((res) => {
                            if (res.code == "200") {
                                this.dialog.visible = false;
                                this.$emit("success", {});
                                this.$message.success(res.msg);
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((res) => {
                            this.$message.error(res.msg);
                        });
            });
        },
    },
};
</script>
